//
//  MutiChooseCollectionView.m
//  Select
//
//  Created by dihuijun on 17/3/20.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "MutiChooseCollectionView.h"
#import "CollectionChooseViewCell.h"
#import "HeaderView.h"

#define ItemHeight 70
#define HeaderHeight 50

static NSString *CellID = @"aCellID";
static NSString *HeaderID = @"aHeaderID";


@implementation MutiChooseCollectionView{
    UICollectionReusableView *resuableView;
}




/**  有header*/
+ (MutiChooseCollectionView *)shareCollectionViewWithFrame:(CGRect)frame HeaderTitle:(NSString *)title{
    
    MutiChooseCollectionView *mutip = [[MutiChooseCollectionView alloc]initWithFrame:frame andTitle:title];
  
    
    return mutip;
}
/** 头部*/
- (instancetype)initWithFrame:(CGRect)frame andTitle:(NSString *)title
{
    self = [super init];
    if (self) {
        self.frame = frame;
        _chooseArray = [NSMutableArray arrayWithCapacity:0];
       
        //创建
        [self setupHeader];
        
    }
    return self;
}

#pragma mark --- setupHeader
- (void)setupHeader
{
    ///1.创建collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) collectionViewLayout:layout];
    //背景颜色
    self.collectionView.backgroundColor = ColorRGB(0xf7f7f7);
    ///注册body部分
    [self.collectionView registerClass:[CollectionChooseViewCell class] forCellWithReuseIdentifier:CellID];
    ///注册header部分
    [self.collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID];
 
    //竖着
    self.collectionView.showsVerticalScrollIndicator = NO;
    //是否滚动
    self.collectionView.scrollEnabled = YES;
    //代理   数据源
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self addSubview:self.collectionView];
    
    
    
}

#pragma mark ----  <UICollectionViewDelegate,UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datasource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionChooseViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.titleLabel.text = self.datasource[indexPath.row];
    
    //如果点击了全选
    if (_isAllSelectedSwitch) {
        
//        NSLog(@"%zd  === %zd",!_isAllSelected,_isAllSelected);
        [cell updateCellSelectedState:_isAllSelected];
        if (indexPath.row == self.datasource.count) {
            _isAllSelectedSwitch = NO;
        }
    }
    
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionChooseViewCell *cell = (CollectionChooseViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell updateCellSelectedState:!cell.isSelected];
    
    //如果选中了当前
    if (cell.isSelected) {
        [_chooseArray addObject:cell.titleLabel.text];
    }else{
        
//        NSLog(@"0: %@   === %zd",_chooseArray,_chooseArray.count);
        
        [_chooseArray removeObject:cell.titleLabel.text];
//        [_chooseArray removeObjectAtIndex:indexPath.row];
//        NSLog(@"1: %@   === %zd",_chooseArray,_chooseArray.count);
    }
    
    //是否全部点了
    if (_chooseArray.count < self.datasource.count) {
        _isAllSelected = NO;
      //全选按钮去掉
        UIButton *button = (UIButton *)[resuableView viewWithTag:1000];
        button.selected = _isAllSelected;
    }else{
        _isAllSelected = YES;
        //全选按钮去掉
        UIButton *button = (UIButton *)[resuableView viewWithTag:1000];
        button.selected = _isAllSelected;
    }
    
    ///给值
    _block(cell.titleLabel.text,_chooseArray);
    

    
}
///大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return  CGSizeMake(SCREEN_WIDTH/3, ItemHeight);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, HeaderHeight);
}
///头部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        if (resuableView == nil) {
            ///创建可以重用
            resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID forIndexPath:indexPath];
            resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID forIndexPath:indexPath];
            //背景颜色
            resuableView.backgroundColor = [UIColor whiteColor];
            //1.标题
            UILabel *headerTitleLabel = [UILabel new];
            headerTitleLabel.text     = @"全选";
            [resuableView addSubview:headerTitleLabel];
            //布局
            [headerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(resuableView.mas_left).offset(15);
                make.top.mas_equalTo(resuableView.mas_top).offset(0);
                make.height.mas_equalTo(resuableView.mas_height);
            }];
            ///2.选择按钮
            UIButton *buttonAll = [UIButton buttonWithType:UIButtonTypeCustom];
            buttonAll.tag = 1000;
            [buttonAll setImage:[UIImage imageNamed:@"table_UnSelect"] forState:UIControlStateNormal];
            [buttonAll setImage:[UIImage imageNamed:@"table_Selected"] forState:UIControlStateSelected];
            buttonAll.userInteractionEnabled = NO;
            [resuableView addSubview:buttonAll];
            //布局
            [buttonAll mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(resuableView.mas_right).offset(-10);
                make.top.mas_equalTo(resuableView.mas_top).offset(0);
                make.bottom.mas_equalTo(resuableView.mas_bottom).offset(0);
                make.height.mas_equalTo(50);
            }];
            ///3.选择全部
            UIButton *buttom = [UIButton buttonWithType:UIButtonTypeCustom];
            buttom.frame = CGRectMake(0, 0, resuableView.frame.size.width, resuableView.frame.size.height);
            [buttom addTarget:self action:@selector(chooseAllClick:) forControlEvents:UIControlEventTouchUpInside];
            [resuableView addSubview:buttom];
            _isAllSelected = NO;
        }
    }
    
    return resuableView;
}
- (void)chooseAllClick:(UIButton *)button
{
    _isAllSelectedSwitch = YES;
    
    _isAllSelected = !_isAllSelected;
    
    
    UIButton *button1 = (UIButton *)[resuableView viewWithTag:1000];
    
    [button1 setSelected:_isAllSelected];
    
//    NSLog(@"%zd",_isAllSelected);
    if (_isAllSelected) {
         [_chooseArray removeAllObjects];
        [_chooseArray addObjectsFromArray:self.datasource];
    }else{
        [_chooseArray removeAllObjects];
        
    }
    //刷新
    [_collectionView reloadData];
    //
    _block(@"All",_chooseArray);
}




/** 刷新collectionView*/
- (void)reloadData{
    [self.collectionView reloadData];
}

@end
